package light.head.controller.oa;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.AlbumDAO;
import light.head.dao.oa.PhotoDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Album;
import light.head.model.Photo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = Url.C_PHOTOSHOW)
public class PhotoShowController extends SupporterController{
	

	private PhotoDAO photoDAO;
	private AlbumDAO albumDAO;
	private OrganDAO organDAO;

	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.ALBUMSHOW_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
			Map<String, String> map = createQueryMapByParmName(request, Parm.OC);
			Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
			if (albumDAO.findByMap(organDAO.getSonCodeList(oc), map, pb) == RESULT.SUCCESS) {
				Album al = null;
				for (Object o : pb.getResultList()) {
					al = (Album) o;
					al.setPhotoList(photoDAO.findByAlc(al.getCode()));
				}
				request.setAttribute(Parm.PAGE_BEAN, pb);
			}
			return new ModelAndView(Url.ALBUMSHOW_LIST);
	}
	

	@RequestMapping(value = Url.QUERYPHOTO)
	public ModelAndView queryPhoto(Integer alc, HttpServletRequest request, HttpServletResponse response) throws Exception {
			List<Photo> photoList = photoDAO.findByAlc(alc);
			if (photoList != null) {
				request.setAttribute(Parm.LIST, photoList);
				request.setAttribute(Parm.ALC, alc);
			}
			return new ModelAndView(Url.PHOTOSHOW_LIST);
	}
	
	@RequestMapping(value = Url.PHOTOSHOW)
	public ModelAndView photoShow(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			Photo p = photoDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.PHOTO, p);
		}
		return new ModelAndView(Url.PHOTOSHOW_SHOW);
	}
	
	@Autowired
	public void setPhotoDAO(PhotoDAO photoDAO) {
		this.photoDAO = photoDAO;
	}
	@Autowired
	public void setAlbumDAO(AlbumDAO albumDAO) {
		this.albumDAO = albumDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
}
